package br.edu.unifei.cct720.alieninvasion.entity.alien;

import br.edu.unifei.cct720.alieninvasion.entity.Entity;
import br.edu.unifei.cct720.alieninvasion.entity.ship.ShipShotEntity;
import br.edu.unifei.cct720.alieninvasion.main.gui.GameCanvas;
import br.edu.unifei.cct720.alieninvasion.utils.Constants;

/**
 * Class to represent an entity of an alien ship
 * 
 * @author Felipe Knappe
 *
 */
public class AlienShipEntity extends Entity {

	/**
	 * The speed at which the entity moves
	 */
	protected double moveSpeed = 75;

	/**
	 * True if alien ship hasn't vertical movement
	 */
	protected boolean stayStatic;

	/**
	 * The last time that this ship attacked
	 */
	protected long lastAlienAttack = 0;

	/**
	 * The times that this entity was hit by shot
	 */
	protected int fired = 0;

	/**
	 * Class Constructor
	 * Creates
	 * 
	 * @param game The game in which this entity is being created
	 * @param ref  The reference to this entity sprite
	 * @param x    The intial x-coordinate of this entity
	 * @param y    The intial y-coordinate of this entity
	 * @param st   The parameter that tells if this entity moves only horizontally
	 */
	public AlienShipEntity(GameCanvas game, String ref, int x, int y, boolean st) {
		super(game, ref, x, y);

		this.stayStatic = st;
		this.dx = -moveSpeed;

		if(!stayStatic) {
			this.dy = moveSpeed;
		} else {
			this.dy = 0.0;
		}
	}

	/**
	 * Class Constructor 
	 * Create a new alien entity
	 * 
	 * @param game The game in which this entity is being created
	 * @param x    The intial x-coordinate of this entity
	 * @param y    The intial y-coordinate of this entity
	 * @param st   The parameter that tells if this entity moves only horizontally
	 * 
	 */
	public AlienShipEntity(GameCanvas game, int x, int y, boolean st) {
		this(game, Constants.ALIEN_SHIP, x, y, st);	
	}

	/**
	 * Method responsible for request that this entity move
	 * itself based on time elapsed.
	 * 
	 * @param delta The shift value
	 * 
	 */
	public void move(long delta) {

		/* 
		 * If the entity reached the left hand side of the screen and
		 * are moving left then request a logic update
		 */
		if ((dx < 0) && (x < 10)) {
			game.updateLogic();
		}

		/*
		 * Otherwise, if reached the right hand side of the screen
		 * and are moving to right then request a logic update 
		 */
		if ((dx > 0) && (x > 750)) {
			game.updateLogic();
		}

		// proceed with normal move
		super.move(delta);

		/*
		 * If entity reached the bottom of screen,
		 * remove this entity and instantiate a new one
		 */
		if(this.y > 600) {
			game.removeEntity(this);
		}
	}

	/**
	 * Method responsible to update the 
	 * game logic related to aliens.
	 * 
	 */
	public void doLogic() {
		// swap over horizontal movement
		dx = -dx;
	}

	/**
	 * Method responsible for notify this entity that it has collided 
	 * with another entity.
	 * 
	 * @param other The other collided entity
	 * 
	 */
	@Override
	public void collidedWith(Entity other) {
		// if collision occurred with a shot entity, it dies
		if (other instanceof ShipShotEntity) {
			/*
			 *  it is needed to hit this entity some times
			 *  to get it down
			 */
			if(fired == this.game.getAlienShipHits()) {
				game.removeAlienEntity(this);
				game.notifyEnemyKilled();
			} else {
				fired += 1;
			}
		}
	}

	/**
	 * Method responsible for get lastAlienAttack attribute
	 * value.
	 * 
	 * @return long
	 */
	public long getLastAlienAttack() {
		return lastAlienAttack;
	}

	/**
	 * Method responsible for set lastAlienAttack attributes
	 * value.
	 * 
	 * @param lastAlienAttack The value that will be set as allienAttack value
	 * 
	 */
	public void setLastAlienAttack(long lastAlienAttack) {
		this.lastAlienAttack = lastAlienAttack;
	}
}
